package cn.factorybox.item.service.impl;

import cn.factorybox.common.utils.DateFormatUtil;
import cn.factorybox.item.mapper.DeviceDataAppMapper;
import cn.factorybox.item.mapper.DeviceMapper;
import cn.factorybox.item.mapper.JudgeValueMapper;
import cn.factorybox.item.service.DeviceDataAppService;
import cn.factorybox.item.utils.JudgeAppData;
import cn.factorybox.item.utils.JudgeAppDayData;
import cn.factorybox.model.entity.DeviceData;
import cn.factorybox.model.entity.JudgeValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.CompletableFuture;

/**
 * @author flypig
 * @version 1.0
 * @date 2020-03-25 17:37
 */
@Service
public class DeviceDataAppServiceImpl implements DeviceDataAppService {

    @Autowired
    private DeviceDataAppMapper deviceDataAppMapper;

    @Autowired
    private JudgeValueMapper judgeValueMapper;

    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    @Async("factorybox")
    public CompletableFuture<Map<String,Object>> getAppData(String fSerialNumber) throws Exception {
        Map<String,Object> map = new HashMap<>();
        List<JudgeValue> list = new ArrayList<>();
        // 查询设备的类型
        int type = deviceMapper.selectType(fSerialNumber);
        String area = "";
        if (type == 0) {
            area = "办公室";
            list = judgeValueMapper.selectIndoor();
        } else if (type == 1) {
            area = "实验室";
            list = judgeValueMapper.selectExper();
        } else if (type == 2) {
            area = "室外";
            list = judgeValueMapper.selectOutdoor();
        }
        map.put("area",area);
        // 获取最新的一组数据
        List<DeviceData> datas = deviceDataAppMapper.getNewData(fSerialNumber);
        List<Map<String,Object>> more = new ArrayList<>();
        String result = "";
        String time = "";
        if (datas.size() != 0) {
            more = JudgeAppData.formatData(datas.get(0),list,type);
            result = datas.get(0).getResult();
            time = DateFormatUtil.formatDate(datas.get(0).getfCreateTime());
        }
        map.put("final_rank",result);
        map.put("update_time", time);
        map.put("more",more);
        return CompletableFuture.completedFuture(map);
    }

    @Override
    @Async("factorybox")
    public CompletableFuture<Map<String,Object>> getDayHistory(String fSerialNumber) throws Exception {
        Map<String,Object> map = new HashMap<>();
        List<JudgeValue> list = new ArrayList<>();
        // 查询设备的类型
        int type = deviceDataAppMapper.getDeviceType(fSerialNumber);
        // 获取当前天的分钟数为30的数据
        List<DeviceData> todayList = deviceDataAppMapper.getTodayData(fSerialNumber);
        if (type == 0) {
            list = judgeValueMapper.selectIndoor();
        } else if (type == 1) {
            list = judgeValueMapper.selectExper();
        } else if (type == 2) {
            list = judgeValueMapper.selectOutdoor();
        }
        List<Map<String,Object>> day_history = JudgeAppDayData.formatData(type,todayList,list,"day");
        map.put("day_history",day_history);
        return CompletableFuture.completedFuture(map);
    }

    @Override
    @Async("factorybox")
    public CompletableFuture<Map<String,Object>> getWeekHistory(String fSerialNumber) throws Exception {
        Map<String, Object> map = new HashMap<>();
        List<JudgeValue> list = new ArrayList<>();
        // 查询设备的类型
        int type = deviceDataAppMapper.getDeviceType(fSerialNumber);
        if (type == 0) {
            list = judgeValueMapper.selectIndoor();
        } else if (type == 1) {
            list = judgeValueMapper.selectExper();
        } else if (type == 2) {
            list = judgeValueMapper.selectOutdoor();
        }
        // 获取前7天的全部数据
        List<Map<String,Object>> week_history = new ArrayList<Map<String,Object>>();
        Map<String,Object> params = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        Date nowTime = new Date();
        params.put("fSerialNumber",fSerialNumber);
        for (int i = 1; i <= 7; i++) {
            // 遍历查询前i天的数据
            Map<String,Object> weekdatas = new HashMap<>();
            params.put("num",i);
            List<DeviceData> datas = deviceDataAppMapper.getBeforeDayData(params);
            calendar.setTime(nowTime);
            calendar.add(Calendar.DAY_OF_MONTH, -i);
            Date beforeTime = calendar.getTime();
            String time = sdf.format(beforeTime);
            weekdatas.put("week_date",time);
            List<Map<String,Object>> week_details = new ArrayList<>();
//            System.out.println(i);
            if (!datas.isEmpty()) {
                week_details = JudgeAppDayData.formatData(type,datas,list,"week");
            }
            weekdatas.put("week_details",week_details);
            week_history.add(weekdatas);
        }
        map.put("week_history",week_history);
        return CompletableFuture.completedFuture(map);
    }


//    @Async("factorybox")
//    public void executeAsyncTask(int i) throws InterruptedException {
//        long startTime=System.currentTimeMillis();   //获取开始时间
//        Thread.sleep(3000);
//        System.out.println("线程" + Thread.currentThread().getName() + " 执行异步任务：" + i);
//        long endTime=System.currentTimeMillis(); //获取结束时间
//        System.out.println(i + ":程序运行时间： "+(endTime-startTime)+"ms");
//    }
}
